package com.yanqu.road.server.manager.yrank;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.rank.yrank.UserYRank;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossYRankCacheMgr {
    private static Map<Integer, Map<Integer, Map<String, CrossUserYRankCache>>> userRankCacheMap = new ConcurrentHashMap<>();

    public static CrossUserYRankCache getUserYRankCacheNewIfNull(int activityId, int groupId, String idKey) {
        Map<Integer, Map<String, CrossUserYRankCache>> groupMap = userRankCacheMap.get(activityId);
        if (groupMap == null) {
            synchronized (userRankCacheMap) {
                groupMap = userRankCacheMap.get(activityId);
                if (groupMap == null) {
                    groupMap = new ConcurrentHashMap<>();
                    userRankCacheMap.put(activityId, groupMap);
                }
            }
        }
        Map<String, CrossUserYRankCache> idMap = groupMap.get(groupId);
        if (idMap == null) {
            synchronized (groupMap) {
                idMap = groupMap.get(groupId);
                if (idMap == null) {
                    idMap = new ConcurrentHashMap<>();
                    groupMap.put(groupId, idMap);
                }
            }
        }

        if (idMap.get(idKey) == null) {
            synchronized (idMap) {
                if (idMap.get(idKey) == null) {
                    idMap.put(idKey, new CrossUserYRankCache(activityId, groupId, idKey));
                }
            }
        }
        return idMap.get(idKey);
    }

    public static RankProto.RankListMsg.Builder getUserRankList(int activityId, int groupId, String idKey) {
        CrossUserYRankCache userYRankCache = getUserYRankCacheNewIfNull(activityId, groupId, idKey);
        if (userYRankCache.isNeedUpdateRank(3)) {//3秒更新一次
            List<UserYRank> rankList = CrossYRankMgr.getSortUserRankList(activityId, groupId, idKey);

            RankProto.RankListMsg.Builder rankBuilder = RankProto.RankListMsg.newBuilder();
            for (UserYRank rank : rankList) {
                rankBuilder.addRankList(RankProto.RankTempMsg.newBuilder()
                        .setUserId(rank.getUserId())
                        .setParam(rank.getParam())
                        .setValue(rank.getValue().toString())
                        .setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(rank.getUserBaseInfo()))
                );
            }

            userYRankCache.setRankBuilder(rankBuilder);
        }

        return userYRankCache.getRankBuilder();
    }

    public static int getUserRank(int activityId, int groupId, String idKey, RankProto.RankListMsg.Builder rankBuilder, long playerId) {
        int maxRank = rankBuilder.getRankListCount() > 0 ? CrossYRankMgr.getMaxRank(activityId, idKey) : GameConfig.RANK_DEFAULT_MAX_COUNT;

        for (int i = 0; i < rankBuilder.getRankListList().size(); i++) {
            int rank = i + 1;
            if (rank > maxRank) {
                return -1;
            }
            if (rankBuilder.getRankList(i).getUserId() == playerId) {
                return rank;
            }
        }
        return -1;
    }

    /**
     * 过期活动移出内存
     */
    public static void expireCheck() {
        long currentSecond = DateHelper.getCurrentTime() / 1000;
        List<Integer> activityIds = new ArrayList<>(userRankCacheMap.keySet());
        for (Integer activityId : activityIds) {
            ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
            if (activityInfo == null || currentSecond > activityInfo.getEndShowTime()) { //活动过期不需要缓存
                userRankCacheMap.remove(activityId);
            }
        }
    }
}
